package com.msh.platform.elm.service.impl;

import com.msh.platform.bizmedia.base.exception.BusinessException;
import com.msh.platform.bizmedia.base.util.Tools;
import com.msh.platform.elm.dao.DevTestRepository;
import com.msh.platform.elm.entity.DevTest;
import com.msh.platform.elm.service.DevTestService;
import com.msh.platform.elm.vo.DevTestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
@Slf4j
public class DevTestServiceImpl implements DevTestService {

    @Autowired
    private DevTestRepository devTestRepository;

    @Override
    @Transactional
    public void saveDevTest(DevTestVO vo) {
        DevTest devTest = new DevTest();
        BeanUtils.copyProperties(vo,devTest);
        devTest.setUid(UUID.randomUUID().toString());
        Integer currTime = Tools.getCurrTime();
        devTest.setCreateTime(currTime);
        devTestRepository.save(devTest);
        if (log.isDebugEnabled()) {
            log.debug("保存成功");
        }
    }

    @Override
    public void deleteDevTestByName(String name) {
        devTestRepository.deleteByName(name);
        if (log.isDebugEnabled()) {
            log.debug("删除成功");
        }
    }

    @Override
    public DevTestVO getDevTest(String name) {
        DevTest devTest = devTestRepository.findByName(name);
        DevTestVO vo = new DevTestVO();
        BeanUtils.copyProperties(devTest,vo);
        return vo;
    }

    @Override
    public DevTestVO getDevTest(Integer id) {
        DevTest devTest = devTestRepository.findById(id)
                .orElseThrow(() -> new BusinessException(-1,"根据ID没有获取到数据,id:"+id));
        DevTestVO vo = new DevTestVO();
        BeanUtils.copyProperties(devTest,vo);
        return vo;
    }
}
